﻿Type.registerNamespace('Kevin.Controls');

$SitePath = Kevin.Controls.SitePath = function(element) {
    Kevin.Controls.SitePath.initializeBase(this, [element]);
    this.CurrentLink = null;
    this.StateList = null;
    this.MenuList = null;
    this.MenuID = null;
}

$SitePath.prototype = {
    initialize: function() {
        $SitePath.callBaseMethod(this, 'initialize');
        this.loadPath();
        
    },
    dispose: function() {
        $SitePath.callBaseMethod(this, 'dispose');
    },
    loadPath: function() {
        if (!this.MenuList) {
            var menuClient = this.MenuID + '_ClientState';

            this.StateList = Sys.Serialization.JavaScriptSerializer.deserialize($get(menuClient).value);
            this.MenuList = this.StateList.MenuList;
            this.CurrentLink = this.StateList.CurrentLink;
        }
        for (var i = 0; i < this.MenuList.length; i++) {
            //var controlID = "menu" + this.MenuList[i].ID;
            //var control = $get(controlID);
            //if (control) {
            //  $addHandler(control, 'click', Function.createDelegate(this, this.setCurrentLink));
            //}
            
        }
    },
    setCurrentLink: function(obj) {
        for (var i = 0; i < this.MenuList.length; i++) {
            var controlID = "menu" + this.MenuList[i].ID;
            if (obj.target.id == controlID) {
                this.CurrentLink = obj.target.id;
            }
        }
        var v_state = { 'CurrentLink': this.CurrentLink, 'MenuList': this.MenuList }
        this._clientStateField.value = Sys.Serialization.JavaScriptSerializer.serialize(v_state);
        this.saveClientState();
        return true;
    },
    loadClientState: function() {
        if (this._clientStateField.value == '' || this._clientStateField.value == 'undefined') {
            return;
        }
        this.StateList = Sys.Serialization.JavaScriptSerializer.deserialize(this._clientStateField);
    },
    saveClientState: function() {
        return Sys.Serialization.JavaScriptSerializer.serialize(this._clientStateField.value);
    },
    get_clientStateField: function() {
        return this._clientStateField;
    },
    set_clientStateField: function(value) {
        if (this._clientStateField != value) {
            this._clientStateField = value;
            this.raisePropertyChanged('clientStateField');
        }
    },
    get_MenuID: function() {
        return this.MenuID;
    },
    set_MenuID: function(value) {
        this.MenuID = value;
    }
}

$LeftMenu.registerClass('Kevin.Controls.SitePath', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();